/*
 * Copyright 2010 Aekold Helbrass
 * 
 * This file is part of RosterComposer.
 * 
 * RosterComposer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RosterComposer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RosterComposer.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.heresylabs.rostercomposer;

import java.awt.Dimension;
import javax.swing.AbstractSpinnerModel;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import org.heresylabs.rostercomposer.roster.RosterEntry;
import org.heresylabs.rostercomposer.units.Option;

/**
 *
 * @author Aekold Helbrass <Helbrass@gmail.com>
 */
public class OptionSpinner extends OptionComponent {

    private SpinnerModel spinnerModel;
    private JSpinner spinner;

    public OptionSpinner(RosterEntry rosterEntry, Option option) {
        super(rosterEntry, option);
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

        spinnerModel = new OptionsSpinnerModel(rosterEntry, option);
        spinner = new JSpinner(spinnerModel);
        Dimension size = new Dimension(50, spinner.getMinimumSize().height);
        spinner.setMinimumSize(size);
        spinner.setPreferredSize(size);

        super.add(spinner);
        super.add(Box.createHorizontalStrut(6));
        super.add(new JLabel(option.toString()));
        super.add(Box.createHorizontalGlue());

        super.setMaximumSize(super.getPreferredSize());
        checkEnabled();
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        spinner.setEnabled(false);
    }

    @Override
    public void select() {
        spinnerModel.setValue(1);
    }

    @Override
    public void deselect() {
        spinnerModel.setValue(0);
    }

    private static class OptionsSpinnerModel extends AbstractSpinnerModel {

        private final RosterEntry rosterEntry;
        private final Option option;

        public OptionsSpinnerModel(RosterEntry rosterEntry, Option option) {
            this.rosterEntry = rosterEntry;
            this.option = option;
        }

        private int getCurrentValue() {
            return rosterEntry.getOptionsCount(option);
        }

        @Override
        public Object getValue() {
            return getCurrentValue();
        }

        @Override
        public void setValue(Object value) {
            int c = ((Integer) value).intValue();
            if (c != getCurrentValue()) {
                rosterEntry.setOptionsCount(option, c);
                fireStateChanged();
            }
        }

        @Override
        public Object getNextValue() {
            int c = getCurrentValue();
            if (c < option.getMaxNumber() || (option.isAnyModels() && c < rosterEntry.getNumber())) {
                return c + 1;
            }
            return null;
        }

        @Override
        public Object getPreviousValue() {
            int c = getCurrentValue();
            if (c > 0) {
                return c - 1;
            }
            return null;
        }

    }

}
